package 模拟;

public class Z字形变换3 {
    //https://leetcode.cn/problems/zigzag-conversion/description/
    public String convert(String ss, int numRows) {
        if(numRows == 1) return ss;
        char[] s = ss.toCharArray();
        StringBuilder ret = new StringBuilder();
        int l = s.length;
        int d = 2 * numRows - 2;
        for (int i = 0; i < numRows; i++) {
            if(i == 0) {
                for (int j = 0; j < l; j += d) {
                    ret.append(s[j]);
                }
            }else if(i == numRows - 1) {
                for (int j = i; j < l; j += d) {
                    ret.append(s[j]);
                }
            }else {
                for (int j1 = i, j2 = d - i; j2 < l || j1 < l ;j1 += d, j2 += d) {
                    ret.append(s[j1]);
                    if(j2 < l) {
                        ret.append(s[j2]);
                    }
                }
            }
        }
        return ret.toString();
    }
}
